function msg = jstegd(img, seed)
    % JSTEGD  Extract message using JSTEG.
    % MSG = JSTEGD(IMG, START) extracts message from IMG, and return the 
    % message body. It automatically calls BINARYTOARRAY to get the 
    % original message. IMG is a N*64 matrix, each line represents a 8x8 
    % block in DCT-coefs.
    %

    r = int16(im2vec(img, [8 8])');    
    [M,N] = size(r);
    bin = zeros(1,M*N);
    
    % generate pusedo random sequence.
    if nargin < 2
        seed = M*N;
    end
    rs = getRandStream(seed);
    tran = prand3(M*N, rs);

    % get all LSB
    count = 1;
    p = 0;
    for i1=1:M
        for j1=1:N
            p = p + 1;
            t = tran(1,p);% t=1..N*M
            i = floor((t-1) / N)+1;
            j = (t-1) - (i-1)*N + 1;
            if (r(i,j) ~= 0) && (r(i,j) ~= 1)
                bin(count) = mod(r(i,j),2);
                count = count + 1;
            end
        end
    end
    
    count = count - 1;
    
    % output
    msg = binaryToArray(bin, count);
    fprintf(1, 'recovered message is: %s\n',msg);
end
